package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.PddOrdersMapper;
import com.robotserver.orders.param.JdOrderSyncParam;
import com.robotserver.orders.param.PddOrderPullByOrderSnParam;
import com.robotserver.orders.param.PddOrderSyncParam;
import com.robotserver.orders.param.biz.PddOrderPullParam;
import com.robotserver.orders.service.PddOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:42
 * @Description:
 */
@RestController
public class PddOrderController {

    private static Logger logger = LoggerFactory.getLogger(PddOrderController.class);

    @Autowired
    private PddOrderService pddOrderService;
    @Autowired
    private PddOrdersMapper pddOrdersMapper;

    @PostMapping("pddorder/pull")
    public Object pullOrder(PddOrderPullParam param){
        return pddOrderService.getOrderList(param);
    }

    /**
     * 根据订单号获取订单
     * @param orderId
     * @return
     */
    @PostMapping("pddorder/getByOrderId")
    public Object getByOrderId(String orderId){
        return pddOrderService.getByOrderId(orderId);
    }

    /**
     * 拼多多订单拉取-V3版本
     * @param pddOrderSyncParam
     * @return
     */
    @PostMapping("pddorder/remotePullV3")
    public Object remotePullV3(PddOrderSyncParam pddOrderSyncParam){

        logger.info("--3.0手动远程下载拼多多订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(pddOrderSyncParam));

        //判断是否订单号为空，如果不为空，则先修改数据库此订单的最新修改时间
        if(StringUtils.isNotBlank(pddOrderSyncParam.getChildOrderNo())){
            pddOrdersMapper.updateUpdateTime(pddOrderSyncParam.getChildOrderNo());
        }

        pddOrderService.syncOrder(pddOrderSyncParam);
        return true;
    }

    /**
     * 拼多多根据订单号拉取订单
     * @param pddOrderPullByOrderSnParam
     * @return
     */
    @PostMapping("pddorder/pullByOrderSn")
    public Object pullByOrderSn(PddOrderPullByOrderSnParam pddOrderPullByOrderSnParam){
        pddOrderService.pullByOrderSn(pddOrderPullByOrderSnParam);
        return true;
    }

    /**
     * 远程补拉全部联盟订单-3.0V
     * @param pddOrderSyncParam
     * @return
     */
    @PostMapping("pddorder/pullOrderByAllToken")
    public Object pullOrderByAllToken(PddOrderSyncParam pddOrderSyncParam){

        logger.info("--god平台手动远程下载拼多多订单--pddOrderSyncParam={}", JSONObject.toJSONString(pddOrderSyncParam));

        if (pddOrderSyncParam.getBegin_time() == null
                || pddOrderSyncParam.getEnd_time() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        pddOrderService.syncOrder(pddOrderSyncParam);

        return true;
    }
}
